import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '@/views/HomeView.vue'
import MainContent from "@/views/MainContent.vue";
import AboutView from '@/views/AboutView.vue'
import NotFound from "@/views/NotFound.vue";
import Applocation from "@/views/case/Applocation.vue";
import RegisterView from "@/views/RegisterView.vue";
// 新增登录页面导入
import LoginView from '@/views/LoginView.vue';

const routes = [
    // 修改路由配置：将登录页面设为根路径
    {
        path: '/login',
        name: 'Login',
        component: LoginView
    },

    // 主页相关内容改为嵌套在 /home 路径下
    {
        path: '/',
        name: 'MainContent',
        component: MainContent,
        meta: {
            requiresAuth: true // 路由元信息示例
        },
        children: [
            {
                path: '/home',
                name: 'Home',
                component: HomeView
            },
            {
                path: 'about',
                name: 'About',
                component: AboutView
            },
            {
                path: 'appLocation',
                name: 'AppLocation',
                component: Applocation
            },
            {
                path: 'register',
                name: 'Register',
                component: RegisterView
            }
        ]
    },

    {
        path: '/:pathMatch(.*)*', // 404 路由捕获
        name: 'NotFound',
        component: NotFound
    }
    // 添加其他路由...
]

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes
})
// 全局前置守卫
router.beforeEach((to, from, next) => {
    console.log('路由跳转前触发')
    const requiresAuth = to.matched.some(record => record.meta?.requiresAuth);
    const isAuthenticated = localStorage.getItem('authToken'); // 假设你使用 localStorage 来存储 token
    if (requiresAuth && !isAuthenticated) {
        next('/login'); // 未登录，重定向到登录页
    } else {
        next(); // 已登录，继续导航
    }
})
export default router
